local newgt = {}                     -- create new environment
setmetatable(newgt, {__index = _G})
test = newgt                         -- set environment to be name of map
--setfenv(1, newgt)

-- Audio vectors
sound_filenames = {"sound1.flac"}
music_filenames = {"happyTown.flac"}
-- Reference to the mode which this script describes
mode = {}
dialogueManager = {}
eventManager = {}

-- for test file, assume gender is female because that's the sprite I have
hero = nil

-- Load map objects, events, et cetera

function Init(m)
  mode = m;
  dialogueManager = m.getDialogueManager;
  eventManager = m.getEventManager;
  -- Add Sprites
  hero = CreateCharacter("walking_guy", 1, 20*32, 20*32);
  hero:setDirection(2);
--  hero:setMoveSpeed(250);
  mode:addCharacter(hero);
  mode:setPlayerCharacter(hero);
  mode:setCamera(hero);
  createNPCs();
  addChest(200,400,0,1, 1, 0, 0);
  createTriggerAreas();
  createEvents();
print(GlobalState:getGold());
  if (GlobalState:doesEventExist("story", "opening_finished") == false) then
    -- Since this is the first instance (being the test case) add hero to party
    GlobalState:addCharacter(hero);
    GlobalState:setEventValue("story", "opening_finished", 100);
print("no opening!");
  end
end

function createNPCs() 
  local sprite;
  local dialogue;
  -- New NPC
  sprite = CreateCharacter("hero_f", 200, 40*32, 25*32); 
--  sprite:setCollide(true);
  mode:addCharacter(sprite);
  sprite:setControlType(3);
  dialogue = OForce_Mode.Dialogue(1);
  dialogue:addLine("Hi There", 200);
  dialogue:addLine("This is a test", 200);
  dialogueManager:addDialogue(dialogue);
  sprite:addDialogueByID(1);
  -- New NPC
  sprite = CreateCharacter("nude_female_elf", 201, 12*32,11*32);
  mode:addCharacter(sprite);
  sprite:setControlType(1);
  dialogue = OForce_Mode.Dialogue(2);
  dialogue:addLine("I lost all of my clothing!", 201);
  dialogue:addLine("Please help me draw my clothes.\nIt's cold!\n",201);
  dialogueManager:addDialogue(dialogue);
  sprite:addDialogueByID(2);
  -- New NPC
  sprite = CreateCharacter("walking_guy", 202, 40*32, 13*32);
  sprite:setAnimDuration(0.75);
  sprite:setControlType(3);
  mode:addCharacter(sprite);
  dialogue = OForce_Mode.Dialogue(3);
  dialogue:addLine("Don't believe the rumors. I am not you.", 202);
  dialogueManager:addDialogue(dialogue);
  sprite:addDialogueByID(3);
end

function createEvents()
  local event = {}
  event = OForce_Mode.MoveCharacterEvent("Move naked elf1", 201, 22*32, 14*32);
  event:addEventAtEndWithDelay("Move naked elf2", 0.7);
  eventManager:addEvent(event);
  event = OForce_Mode.MoveCharacterEvent("Move naked elf2", 201, 12*32, 11*32);
  event:addEventAtEndWithDelay("Move naked elf1", 1.2); -- Loop
  eventManager:addEvent(event);
  eventManager:startEvent("Move naked elf1", 1);
  -- exit event
  event = OForce_Mode.MapTransitionEvent("To Test Map2", "test2", "From Test Map", false);
  eventManager:addEvent(event);
end

local exit_area = {};
function createTriggerAreas()
  exit_area = OForce_Mode.TriggerArea(0.0, 768, 32, 640);
  mode:addTriggerArea(exit_area);
end

function checkTriggers()
  if (exit_area:enteredArea() == true) then
     eventManager:startEvent("To Test Map2");
  end
end

function addChest(x, y, gold, chestID, itemID, isOpen, isHidden)
  -- If chest has an item, there will be no gold dropped.
  chest = OForce_Object.Treasure(x, y, gold, chestID, itemID, isOpen, isHidden);
  chest:setCurrentLayer(0);
  mode:addTreasure(chest);
end

function update() 
  checkTriggers();
end



map_functions = {}

